package com.example.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    public final static String SIMPLE = "simple";   // 队列名称
    public final static String PUB_SUB_EXCHANGE = "pub_sub"; // 发布订阅的交换机
    public final static String ROUTING_EXCHANGE = "routing1"; // 发布订阅的交换机
    public final static String TOPIC_EXCHANGE = "topic"; // 发布订阅的交换机
    public final static String QUEUE_1 = "queue_1"; //队列1
    public final static String QUEUE_2 = "queue_2"; //队列2
    public final static String ROUTING_1 = "routing_1"; //routing队列1
    public final static String ROUTING_2 = "routing_2"; //routing队列2
    public final static String TOPIC_1 = "routing_1"; //topic队列1
    public final static String TOPIC_2 = "routing_2"; //topic队列2



    @Bean
    public Queue simpleQueue() {
        return new Queue(SIMPLE, true, false, true);
    }

    @Bean("queue_1")
    public Queue QUEUE_1_Queue() {
        return new Queue(QUEUE_1, true, false, true);
    }
    @Bean("queue_2")
    public Queue QUEUE_2_Queue() {
        return new Queue(QUEUE_2, true, false, true);
    }
    @Bean("routing_1")
    public Queue OUTING_1_Queue() {
        return new Queue(ROUTING_1, true, false, true);
    }

    @Bean("routing_2")
    public Queue ROUTING_2_Queue() {
        return new Queue(ROUTING_2, true, false, true);
    }

    @Bean(TOPIC_1)
    public Queue TOPIC_1_Queue() {
        return new Queue(TOPIC_1, true, false, true);
    }

    @Bean(TOPIC_2)
    public Queue TOPIC_2_Queue() {
        return new Queue(TOPIC_2, true, false, true);
    }

    @Bean("routing1")
    public Exchange routingExchange(){
        return ExchangeBuilder.directExchange(ROUTING_EXCHANGE).build();
    }

    @Bean("pub_sub")
    public Exchange pub_sub_Exchange(){
        return ExchangeBuilder.fanoutExchange(PUB_SUB_EXCHANGE).build();
    }

    @Bean(TOPIC_EXCHANGE)
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).build();
    }


    @Bean
    public Binding binding_QUEUE_1(@Qualifier("queue_1") Queue queue, @Qualifier(PUB_SUB_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
    @Bean
    public Binding binding_QUEUE_2(@Qualifier("queue_2") Queue queue, @Qualifier(PUB_SUB_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    @Bean
    public Binding binding_Routing_QUEUE_1(@Qualifier(ROUTING_1) Queue queue, @Qualifier(ROUTING_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_1).noargs();
    }
    @Bean
    public Binding binding_Routing_QUEUE_2(@Qualifier(ROUTING_2) Queue queue, @Qualifier(ROUTING_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_2).noargs();
    }

    @Bean
    public Binding binding_Topic_QUEUE_1(@Qualifier(TOPIC_1) Queue queue, @Qualifier(TOPIC_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("aa.bb.cc").noargs();
    }
    @Bean
    public Binding binding_Topic_QUEUE_2(@Qualifier(TOPIC_2) Queue queue, @Qualifier(TOPIC_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("aa.#").noargs();
    }

}
